<template>
  <div>
    <el-card>
      <el-button type="primary" @click="showAdd = true">添加员工</el-button>
      <common-table :data="employeeList" :columns="columns">
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="edit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="remove(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </common-table>
    </el-card>
    <el-dialog :visible.sync="showAdd" title="添加员工">
      <el-form :model="form">
        <el-form-item label="姓名"><el-input v-model="form.name" /></el-form-item>
        <el-form-item label="部门"><el-input v-model="form.department" /></el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="showAdd = false">取消</el-button>
        <el-button type="primary" @click="add">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapState, mapActions } from 'vuex'
export default {
  components: { CommonTable: () => import('../components/CommonTable.vue') },
  data() {
    return {
      showAdd: false,
      form: { name: '', department: '' },
      columns: [
        { prop: 'name', label: '姓名' },
        { prop: 'department', label: '部门' }
      ]
    }
  },
  computed: {
    ...mapState('employee', ['list']),
    employeeList() { return this.list }
  },
  created() {
    this.fetchList()
  },
  methods: {
    ...mapActions('employee', ['fetchList']),
    add() {
      // 这里应调用API添加员工
      this.showAdd = false
      this.fetchList()
    },
    edit(row) { /* ... */ },
    remove(row) { /* ... */ }
  }
}
</script>